package kubernetes

import (
	"fmt"
	"k8s-admin/global"
	"k8s-admin/service/kubernetes"
	"k8s-admin/utils"
	"net/http"
	"os"
	"strings"

	"github.com/gin-gonic/gin"
)

/*通过手动编写yanl或则编写模板来创建Deployment资源*/
func CreateK8sClusterDeployment(ctx *gin.Context) {
	params := new(struct {
		Namespace      string `form:"namespace" json:"namespace" binding:"required"`
		DeploymentData string `form:"deployment_data" json:"deployment_data" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBindJSON(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())

	} else {

		err = kubernetes.NewK8sInterface().CreateClusterDeployment(cid, params.Namespace, params.DeploymentData)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "创建成功")
		}
	}
}

func CreateClusterDeploymentFile(ctx *gin.Context) {
	dir, _ := os.Getwd()
	params := new(struct {
		Namespace string `form:"namespace" json:"namespace" binding:"required"`
	})

	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		global.TPLogger.Error(err.Error())
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		file, _ := ctx.FormFile("yaml")

		err = ctx.SaveUploadedFile(file, dir+"/"+file.Filename)
		if err != nil {
			global.TPLogger.Error(fmt.Sprint("文件上传失败：", err.Error()))
		} else {
			err := kubernetes.NewK8sInterface().CreateClusterDeploymentFile(cid, params.Namespace, dir+"/"+file.Filename)
			if err != nil {
				utils.ReturnContext(ctx).Failed("失败", err.Error())
			} else {
				utils.ReturnContext(ctx).Successful("成功", "创建成功")
			}
		}
	}
}

/*获取Deployment列表数据*/
func K8sClusterDeployment(ctx *gin.Context) {
	params := new(struct {
		Namespace string `form:"namespace" json:"namespace" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		list, err := kubernetes.NewK8sInterface().GetClusterDeploymentList(cid, params.Namespace)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", list)
		}
	}
}
func K8sClusterDeploymentUp(ctx *gin.Context) {
	params := new(struct {
		Namespace      string `form:"namespace" json:"namespace" binding:"required"`
		DeploymentData string `form:"deployment_data" json:"deployment_data" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		err := kubernetes.NewK8sInterface().UpdateClusterDeployment(cid, params.Namespace, params.DeploymentData)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "更新成功")
		}
	}
}

/*删除Deployment资源*/
func K8sClusterDeleteDeployment(ctx *gin.Context) {
	params := new(struct {
		Namespace      string `form:"namespace" json:"namespace" binding:"required"`
		DeploymentName string `form:"deployment_name" json:"deployment" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		err := kubernetes.NewK8sInterface().DeleteClusterDeployment(cid, params.Namespace, params.DeploymentName)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "删除成功")
		}
	}
}

/*获取某个deployment数据*/
func GetK8sClusterDeployment(ctx *gin.Context) {
	params := new(struct {
		Namespace      string `form:"namespace" json:"namespace" binding:"required"`
		DeploymentName string `form:"deployment_name" json:"deployment_name" binding:"required"`
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBind(params); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		list, err := kubernetes.NewK8sInterface().GetClusterDeployment(cid, params.Namespace, params.DeploymentName)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", list)
		}
	}
}

/*获取yaml模板数据*/
func GetK8sClusterDeploymentYaml(ctx *gin.Context) {
	resource := strings.Split(ctx.Request.URL.String(), "/")[1]
	fmt.Println("URL请求资源地址：", resource)

	yaml, err := utils.ReadDeploymentYaml(resource)
	if err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		utils.ReturnContext(ctx).Successful("成功", yaml)
	}
}

/*
*
图形界面创建deployment资源
*/
func CreateClusterDeploymentUI(ctx *gin.Context) {
	params := new(struct {
		Namespace      string `form:"namespace" json:"namespace" binding:"required"`
		DeploymentData string `form:"deployment_data" json:"deployment_data" binding:"required"` // 这里接收的是前端传递的BASE64编码的YAML数据
	})
	cid := ctx.Param("cid")
	if err := ctx.ShouldBindJSON(params); err != nil {
		utils.ReturnContext(ctx).Failed("失败", err.Error())
	} else {
		err = kubernetes.NewK8sInterface().CreateClusterDeploymentUI(cid, params.Namespace, params.DeploymentData)
		if err != nil {
			utils.ReturnContext(ctx).Failed("失败", err.Error())
		} else {
			utils.ReturnContext(ctx).Successful("成功", "创建成功")
		}
	}
}
